﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WhittleList.Bll
{
    public class UserRepository : BaseRepository, WhittleList.Bll.IUserRepository
    {
        private IQueryable<User> _Users;

        public UserRepository() : this (new WhittleListDataContext())
        {
        }

        public UserRepository(IWhittleListDataContext dbContext) : base(dbContext)
        {
            _Users = dbContext.Users.Where(x => !x.Deleted);
        }

        public bool AuthenticateUser(string emailAddress, string password)
        {
            var user = GetByEmailAddress(emailAddress);
            if (user != null)
            {
                return user.AuthenticatePassword(password);
            }
            else
            {
                return false;
            }
        }

        public void Insert(User user)
        {
            if (_DBContext != null)
            {
                _DBContext.Insert(user);
            }
        }

        public User GetByEmailAddress(string emailAddress)
        {
            return _Users.SingleOrDefault(user => user.EmailAddress == emailAddress);
        }
    }
}
